package com.learn.springlearn.convert;


import com.learn.springlearn.entity.User;
import com.learn.springlearn.entity.UserDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Mapper
public interface UserConvert {

    UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);


    @Mappings(
            {

                     @Mapping(target = "localTime",expression = "java(dateToLocalTime(user.getCreateTime()))")})
    UserDTO convertToDTO(User user);

    List<UserDTO> converToDTOList(List<User> users);


    default LocalTime dateToLocalTime(Date date){
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }
}
